/// 情绪记录数据模型
/// Emotion record data model
class EmotionRecord {
  /// 记录唯一标识符
  /// Unique identifier for the record
  final String id;
  
  /// 情绪类型
  /// Emotion type
  final EmotionType emotionType;
  
  /// 情绪强度 (1-10)
  /// Emotion intensity (1-10)
  final int emotionIntensity;
  
  /// 日记内容
  /// Diary content
  final String? diaryContent;
  
  /// 创建时间
  /// Creation time
  final DateTime createdAt;
  
  /// 更新时间
  /// Update time
  final DateTime updatedAt;
  
  /// 关联的媒体文件列表
  /// Associated media files list
  final List<MediaFile>? mediaFiles;
  
  /// 音频文件路径
  /// Audio file path
  final String? audioPath;
  
  /// 图片文件路径列表
  /// Image file paths list
  final List<String>? imagePaths;
  
  /// 标签列表
  /// Tags list
  final List<String>? tags;

  const EmotionRecord({
    required this.id,
    required this.emotionType,
    required this.emotionIntensity,
    this.diaryContent,
    required this.createdAt,
    required this.updatedAt,
    this.mediaFiles,
    this.audioPath,
    this.imagePaths,
    this.tags,
  });

  /// 从JSON创建实例
  /// Create instance from JSON
  factory EmotionRecord.fromJson(Map<String, dynamic> json) {
    return EmotionRecord(
      id: json['id'] as String,
      emotionType: EmotionType.fromString(json['emotion_type'] as String),
      emotionIntensity: json['emotion_intensity'] as int,
      diaryContent: json['diary_content'] as String?,
      createdAt: DateTime.fromMillisecondsSinceEpoch(json['created_at'] as int),
      updatedAt: DateTime.fromMillisecondsSinceEpoch(json['updated_at'] as int),
      audioPath: json['audio_path'] as String?,
      imagePaths: (json['image_paths'] as List<dynamic>?)?.cast<String>(),
      tags: (json['tags'] as List<dynamic>?)?.cast<String>(),
    );
  }

  /// 转换为JSON
  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'emotion_type': emotionType.value,
      'emotion_intensity': emotionIntensity,
      'diary_content': diaryContent,
      'created_at': createdAt.millisecondsSinceEpoch,
      'updated_at': updatedAt.millisecondsSinceEpoch,
      'audio_path': audioPath,
      'image_paths': imagePaths,
      'tags': tags,
    };
  }

  /// 复制并修改部分属性
  /// Copy with modified properties
  EmotionRecord copyWith({
    String? id,
    EmotionType? emotionType,
    int? emotionIntensity,
    String? diaryContent,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<MediaFile>? mediaFiles,
    String? audioPath,
    List<String>? imagePaths,
    List<String>? tags,
  }) {
    return EmotionRecord(
      id: id ?? this.id,
      emotionType: emotionType ?? this.emotionType,
      emotionIntensity: emotionIntensity ?? this.emotionIntensity,
      diaryContent: diaryContent ?? this.diaryContent,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      mediaFiles: mediaFiles ?? this.mediaFiles,
      audioPath: audioPath ?? this.audioPath,
      imagePaths: imagePaths ?? this.imagePaths,
      tags: tags ?? this.tags,
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is EmotionRecord && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;

  /// 获取情绪强度（兼容性getter）
  /// Get emotion intensity (compatibility getter)
  int get intensity => emotionIntensity;

  @override
  String toString() {
    return 'EmotionRecord(id: $id, emotionType: $emotionType, emotionIntensity: $emotionIntensity, createdAt: $createdAt)';
  }
}

/// 情绪类型枚举
/// Emotion type enumeration
enum EmotionType {
  happy('happy', '开心', '😊'),
  sad('sad', '悲伤', '😢'),
  anxious('anxious', '焦虑', '😰'),
  calm('calm', '平静', '😌'),
  angry('angry', '愤怒', '😠'),
  excited('excited', '兴奋', '🤩'),
  tired('tired', '疲惫', '😴'),
  confused('confused', '困惑', '😕'),
  grateful('grateful', '感激', '🙏'),
  lonely('lonely', '孤独', '😔'),
  content('content', '满足', '😊');

  const EmotionType(this.value, this.label, this.emoji);

  /// 数据库存储值
  /// Database storage value
  final String value;
  
  /// 显示标签
  /// Display label
  final String label;
  
  /// 表情符号
  /// Emoji
  final String emoji;

  /// 从字符串值创建枚举
  /// Create enum from string value
  static EmotionType fromString(String value) {
    return EmotionType.values.firstWhere(
      (type) => type.value == value,
      orElse: () => EmotionType.calm,
    );
  }
}

/// 媒体文件数据模型
/// Media file data model
class MediaFile {
  /// 文件唯一标识符
  /// Unique identifier for the file
  final String id;
  
  /// 关联的记录ID
  /// Associated record ID
  final String recordId;
  
  /// 文件类型
  /// File type
  final MediaFileType fileType;
  
  /// 文件路径
  /// File path
  final String filePath;
  
  /// 文件大小（字节）
  /// File size in bytes
  final int fileSize;
  
  /// 创建时间
  /// Creation time
  final DateTime createdAt;

  const MediaFile({
    required this.id,
    required this.recordId,
    required this.fileType,
    required this.filePath,
    required this.fileSize,
    required this.createdAt,
  });

  /// 从JSON创建实例
  /// Create instance from JSON
  factory MediaFile.fromJson(Map<String, dynamic> json) {
    return MediaFile(
      id: json['id'] as String,
      recordId: json['record_id'] as String,
      fileType: MediaFileType.fromString(json['file_type'] as String),
      filePath: json['file_path'] as String,
      fileSize: json['file_size'] as int,
      createdAt: DateTime.fromMillisecondsSinceEpoch(json['created_at'] as int),
    );
  }

  /// 转换为JSON
  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'record_id': recordId,
      'file_type': fileType.value,
      'file_path': filePath,
      'file_size': fileSize,
      'created_at': createdAt.millisecondsSinceEpoch,
    };
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is MediaFile && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}

/// 媒体文件类型枚举
/// Media file type enumeration
enum MediaFileType {
  image('image', '图片'),
  audio('audio', '音频');

  const MediaFileType(this.value, this.label);

  /// 数据库存储值
  /// Database storage value
  final String value;
  
  /// 显示标签
  /// Display label
  final String label;

  /// 从字符串值创建枚举
  /// Create enum from string value
  static MediaFileType fromString(String value) {
    return MediaFileType.values.firstWhere(
      (type) => type.value == value,
      orElse: () => MediaFileType.image,
    );
  }
}